package com.cloud.filter;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class CloudBaseHeaderFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HeaderMapRequestWrapper headerMapRequestWrapper = new HeaderMapRequestWrapper(req);
        String traceId = req.getHeader("traceId");
        if (StringUtils.isEmpty(traceId)) {
            headerMapRequestWrapper.addHeader("traceId", "123456");
            servletRequest = headerMapRequestWrapper;
        }
        servletRequest.setAttribute("traceId", traceId);
//            BaseContextUtil.set(GlobalMsgCodeConstant.TRACE_ID, traceId);
        filterChain.doFilter(servletRequest, servletResponse);
    }
}